/**  

* @Title: LoginAction.java 

* @Package com.gxbadge.action 

* @Description: TODO(用一句话描述该文件做什么) 

* @author CST

* @version V1.0  

*/ 

package com.gxbadge.action.front;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.gxbadge.action.base.BaseAction;
import com.gxbadge.model.Article;
import com.gxbadge.model.Image;
import com.gxbadge.model.ImageInfo;
import com.gxbadge.model.Language;
import com.gxbadge.model.Navigation;
import com.gxbadge.vo.ImageVo;

public class FrontAction extends BaseAction {
	
	
	private Language lang;
	
	public Language getLang() {
		return lang;
	}
	public void setLang(Language lang) {
		this.lang = lang;
	}
	//语言切换
	public String change_lang() throws Exception {
		
		if(this.getLang()!=null&&this.getLang().getName()!=null){
			
			String langName = this.getLang().getName();
			
			if(langName.equals("zh_CN")){
				
				Locale newLocale = new Locale("zh","CN");
				Locale.setDefault(newLocale);
				
			}else if(langName.equals("en_US")){
				
				Locale newLocale = new Locale("en","US");
				Locale.setDefault(newLocale);
	
			}else{
				
				Locale newLocale = new Locale("zh","CN");
				Locale.setDefault(newLocale);	
			}
			this.setLang(null);
		}

		return "lang_change_success";
	}
	
	private Navigation nav;
	private List<Navigation> navList;
	private Map<Navigation, List<Navigation>> navMap;
	public Navigation getNav() {
		return nav;
	}
	public void setNav(Navigation nav) {
		this.nav = nav;
	}
	public List<Navigation> getNavList() {
		return navList;
	}
	public void setNavList(List<Navigation> navList) {
		this.navList = navList;
	}
	
	public Map<Navigation, List<Navigation>> getNavMap() {
		return navMap;
	}
	public void setNavMap(Map<Navigation, List<Navigation>> navMap) {
		this.navMap = navMap;
	}
	public String find_fnavList() throws Exception {
		
		if(this.getNavList()!=null){
			this.setNavList(null);
		}	
		
		String[] navNoteList = {"hp", "au", "nc", "pc", "bp", "cu"};
		
		List<Navigation> nList = new LinkedList<Navigation>();
		if(Locale.getDefault().equals(Locale.CHINA)){
			
			for(int i=0;i<navNoteList.length;i++){
				
				Navigation nav = adminManager.findNavByNote(navNoteList[i]+"_cn");
				if(nav!=null){
					
					nList.add(nav);
				}
			}
			if(nList!=null&&nList.size()>=1){
				this.setNavList(nList);
			}
			nList = null;
			

		} else if(Locale.getDefault().equals(Locale.US)){
			
			for(int i=0;i<navNoteList.length;i++){
				
				Navigation nav = adminManager.findNavByNote(navNoteList[i]+"_en");
				if(nav!=null){
					nList.add(nav);
				}
			}
			if(nList!=null&&nList.size()>=1){
				this.setNavList(nList);
			}
			nList = null;
		}
		navNoteList = null;
		return "fnavList_find_success";
	}
	
	public String find_fprodList() throws Exception{
		
		if(this.getNavList()!=null){
			this.setNavList(null);
		}

		String navNote = "pc";
		if(Locale.getDefault().equals(Locale.CHINA)){
			
			Navigation nav = adminManager.findNavByNote(navNote+"_cn");
			if(nav!=null){
				
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					this.setNavList(nList);
				}
			}
		}else if(Locale.getDefault().equals(Locale.US)){
			
			Navigation nav = adminManager.findNavByNote(navNote+"_en");
			if(nav!=null){
				
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					this.setNavList(nList);
				}
			}
		}
		
		
		return "fprodList_find_success";
	}
	
	private Image image;
	private List<Image> imageList;
	
	public Image getImage() {
		return image;
	}
	public void setImage(Image image) {
		this.image = image;
	}
	public List<Image> getImageList() {
		return imageList;
	}
	public void setImageList(List<Image> imageList) {
		this.imageList = imageList;
	}
	
	private ImageVo imageVo;
	private List<ImageVo> imageVoList;
	public ImageVo getImageVo() {
		return imageVo;
	}
	public void setImageVo(ImageVo imageVo) {
		this.imageVo = imageVo;
	}
	
	public List<ImageVo> getImageVoList() {
		return imageVoList;
	}
	public void setImageVoList(List<ImageVo> imageVoList) {
		this.imageVoList = imageVoList;
	}
	public String find_fnewProdList() throws Exception{
		
		if(this.getImageVoList()!=null){
			this.setImageVoList(null);
		}
		
		String navNote = "pc";
		if(Locale.getDefault().equals(Locale.CHINA)){
			
			Navigation nav = adminManager.findNavByNote(navNote+"_cn");
			if(nav!=null){
				this.setNav(nav);
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					
					int offset = 0;
					int pageSize = 12;
					List<Image> imageList = adminManager.findImageByNavListAndPage(nList, offset, pageSize);
					List<ImageVo> ivList = new LinkedList<ImageVo>();
					if(imageList!=null&&imageList.size()>=1){
						
						for(int i=0;i<imageList.size();i++){
							ImageVo imageVo = new ImageVo();
							ImageInfo imageInfo = adminManager.findImageInfoByImage(imageList.get(i));
							imageVo.setImageId(imageList.get(i).getImageId());
							imageVo.setImageSavePath(imageList.get(i).getImageSavePath());
							imageVo.setImageTitle(imageInfo.getTitle());
							ivList.add(imageVo);
						}
						if(ivList!=null&&ivList.size()>=1){
							this.setImageVoList(ivList);
						}
					}
					ivList = null;
				}
			}
		}else if(Locale.getDefault().equals(Locale.US)) {
			
			Navigation nav = adminManager.findNavByNote(navNote+"_en");
			if(nav!=null){
				this.setNav(nav);
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					
					int offset = 0;
					int pageSize = 12;
					List<Image> imageList = adminManager.findImageByNavListAndPage(nList, offset, pageSize);
					List<ImageVo> ivList = new LinkedList<ImageVo>();
					if(imageList!=null&&imageList.size()>=1){
						
						for(int i=0;i<imageList.size();i++){
							ImageVo imageVo = new ImageVo();
							ImageInfo imageInfo = adminManager.findImageInfoByImage(imageList.get(i));
							imageVo.setImageId(imageList.get(i).getImageId());
							imageVo.setImageSavePath(imageList.get(i).getImageSavePath());
							imageVo.setImageTitle(imageInfo.getTitle());
							ivList.add(imageVo);
						}
						if(ivList!=null&&ivList.size()>=1){
							this.setImageVoList(ivList);
						}
					}
					ivList = null;
				}
			}
		}
		
		return "fnewProdList_find_success";
	}
	
	private Article arti;
	private List<Article> artiList;
	public Article getArti() {
		return arti;
	}
	public void setArti(Article arti) {
		this.arti = arti;
	}
	public List<Article> getArtiList() {
		return artiList;
	}
	public void setArtiList(List<Article> artiList) {
		this.artiList = artiList;
	}
	public String find_fnewsList() throws Exception{
		
		if(this.getArtiList()!=null){
			this.setArtiList(null);
		}
		
		String navNote = "nc";
		if(Locale.getDefault().equals(Locale.CHINA)){
			
			Navigation nav = adminManager.findNavByNote(navNote+"_cn");
			if(nav!=null){
				
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					
					int offset = 0;
					int pageSize = 6;
					List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, pageSize);
					if(aList!=null&&aList.size()>=1){
						this.setArtiList(aList);
					}
				}
			}
		}else if(Locale.getDefault().equals(Locale.US)){
			
			Navigation nav = adminManager.findNavByNote(navNote+"_en");
			if(nav!=null){
				
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					
					int offset = 0;
					int pageSize = 6;
					List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, pageSize);
					if(aList!=null&&aList.size()>=1){
						this.setArtiList(aList);
					}
				}
			}
			
		}
		
		return "fnewsList_find_success";
	}
	
	private ImageInfo imageInfo;

	public ImageInfo getImageInfo() {
		return imageInfo;
	}
	public void setImageInfo(ImageInfo imageInfo) {
		this.imageInfo = imageInfo;
	}
	public String find_fimageByImageId() throws Exception{
		
		if(this.getImage()!=null&&this.getImage().getImageId()!=null){
			
			Image image = adminManager.findImageByImageId(this.getImage().getImageId());
			if(image!=null){
				this.setImage(image);
				ImageInfo imageInfo = adminManager.findImageInfoByImage(image);
				if(imageInfo!=null){
					this.setImageInfo(imageInfo);
				}
			}
		}

		return "fimageByImageId_find_success";
	}
	
	
	private static int pageSize=12;
	private int pageNo;
	private long totalCount;
	private int pageCount;
	
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public long getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
	}
	public int getPageCount() {
		return pageCount;
	}
	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}
	public int getPageSize() {
		return pageSize;
	}
	
	
	
	public String find_fimageListByNav() throws Exception{
		
		if(this.getImageVoList()!=null){
			this.setImageVoList(null);
		}
		if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				this.setNav(nav);
				
				long imageCountByNav = adminManager.f_find_imageCountByNav(nav);
				this.setTotalCount(imageCountByNav);
				this.setPageCount((int) (this.getTotalCount()/this.getPageSize()+1));
				if(imageCountByNav==0){
					this.setPageCount(0);
				}
				if(this.getPageCount()==0){
					this.setPageNo(0);
				}
				if(this.getPageNo()<1){
					this.setPageNo(1);
				}
				if(this.getPageNo()>this.getPageCount()){
					this.setPageNo(getPageCount());
				}
				if(this.getPageNo()==1){
					
					int offset = 0;
					List<Image> imageList = adminManager.f_findImageByNavAndPage(nav, offset, this.getPageSize());
					List<ImageVo> ivList = new LinkedList<ImageVo>();
					if(imageList!=null&&imageList.size()>=1){
						
						for(int i=0;i<imageList.size();i++){
							ImageVo imageVo = new ImageVo();
							ImageInfo imageInfo = adminManager.findImageInfoByImage(imageList.get(i));
							imageVo.setImageId(imageList.get(i).getImageId());
							imageVo.setImageSavePath(imageList.get(i).getImageSavePath());
							imageVo.setImageTitle(imageInfo.getTitle());
							ivList.add(imageVo);
						}
						if(ivList!=null&&ivList.size()>=1){
							this.setImageVoList(ivList);
						}
					}
					ivList = null;
				}
				if(this.getPageNo()>1&&this.getPageNo()<=this.getPageCount()){
					
					int offset = (this.getPageNo()-1)*this.getPageNo();
					List<Image> imageList = adminManager.f_findImageByNavAndPage(nav, offset, this.getPageSize());
					List<ImageVo> ivList = new LinkedList<ImageVo>();
					if(imageList!=null&&imageList.size()>=1){
						
						for(int i=0;i<imageList.size();i++){
							ImageVo imageVo = new ImageVo();
							ImageInfo imageInfo = adminManager.findImageInfoByImage(imageList.get(i));
							imageVo.setImageId(imageList.get(i).getImageId());
							imageVo.setImageSavePath(imageList.get(i).getImageSavePath());
							imageVo.setImageTitle(imageInfo.getTitle());
							ivList.add(imageVo);
						}
						if(ivList!=null&&ivList.size()>=1){
							this.setImageVoList(ivList);
						}
					}
					ivList = null;
				}
			}

		}

		return "fimageListByNav_find_success";
	}
	
	private Map<Navigation, List<Article>> navArtiMap;
	
	public Map<Navigation, List<Article>> getNavArtiMap() {
		return navArtiMap;
	}
	public void setNavArtiMap(Map<Navigation, List<Article>> navArtiMap) {
		this.navArtiMap = navArtiMap;
	}
	public String find_fartiListByNav() throws Exception{
		
		if(this.getArtiList()!=null){
			this.setArtiList(null);
		}
		if(this.getNav()!=null&&this.getNav().getNavId()!=null){
			
			Navigation nav = adminManager.findNavByNavId(this.getNav().getNavId());
			if(nav!=null){
				
				List<Navigation> nList = adminManager.findNavListByNavPid(nav.getNavId());
				if(nList!=null&&nList.size()>=1){
					
					long artiTotalCount = adminManager.ffind_artiTotalCount(nList);
					this.setTotalCount(artiTotalCount);
					if(this.getTotalCount()==0){
						this.setPageCount(0);
					}
					if(this.getPageCount()==0){
						this.setPageNo(0);
					}
					if(this.getPageNo()<1){
						this.setPageNo(1);
					}
					if(this.getPageNo()>this.getPageCount()){
						this.setPageNo(getPageCount());
					}
					if(this.getPageNo()==1){
						
						int offset = 0;
						List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, this.getPageSize());
						if(aList!=null&&aList.size()>=1){
							this.setArtiList(aList);
						}
						
					}
					if(this.getPageNo()>1&&this.getPageNo()<this.getPageCount()){
						
						int offset = (this.getPageNo()-1)*this.getPageSize();
						List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, this.getPageSize());
						if(aList!=null&&aList.size()>=1){
							this.setArtiList(aList);
						}
					}

				}else{
					
					
					List<Navigation> nl = new LinkedList<Navigation>();
					nl.add(nav);
					long artiTotalCount = adminManager.ffind_artiTotalCount(nl);
					this.setTotalCount(artiTotalCount);
					if(this.getTotalCount()==0){
						this.setPageCount(0);
					}
					if(this.getPageCount()==0){
						this.setPageNo(0);
					}
					if(this.getPageNo()<1){
						this.setPageNo(1);
					}
					if(this.getPageNo()>this.getPageCount()){
						this.setPageNo(getPageCount());
					}
					if(this.getPageNo()==1){
						
						int offset = 0;
						List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, this.getPageSize());
						if(aList!=null&&aList.size()>=1){
							this.setArtiList(aList);
						}
						
					}
					if(this.getPageNo()>1&&this.getPageNo()<this.getPageCount()){
						
						int offset = (this.getPageNo()-1)*this.getPageSize();
						List<Article> aList = adminManager.findFrontArtiListByNavList(nList, offset, this.getPageSize());
						if(aList!=null&&aList.size()>=1){
							this.setArtiList(aList);
						}
					}
					nl = null;
				}
				
			}

		}
		
		return "fartiListByNav_find_success";
	}
	
	public String find_fartiByArtiId() throws Exception{
		
		if(this.getArti()!=null&&this.getArti().getArtiId()!=null){
			
			Article arti = adminManager.findArtiByArtiId(this.getArti().getArtiId());
			if(arti!=null){
				this.setArti(arti);
			}

		}

		return "fartiByArtiId_find_success";
	}
	
	
	
	
	
	
	
	
}
